import React from 'react';
import { Modal, message } from 'antd';
import { FormPanel, FormPanelBtnAlign, FormPanelBtnDisplay, type FormPanelObject, Utils } from 'lfun-bg-comp';
import { Org, createOrg } from '@/service/unit/org';

const Groups: React.FC<{
  show: boolean;
  onCancel: (e?: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
  onOk: Function;
  currentData: Org;
}> = ({ show, onCancel, onOk, currentData }) => {
  return (
    <Modal title="添加" open={show} footer={null} onCancel={onCancel}>
      <FormPanel
        btnsAlign={FormPanelBtnAlign.center}
        restBtnText="返回"
        submitBtnText="确定"
        buttonsDisplay={FormPanelBtnDisplay.block}
        onFinish={async (formPanelObject: FormPanelObject) => {
          const res = await createOrg({
            ...formPanelObject.getFieldsValue(),
            parent_id: currentData.id,
            school_id: Utils.getProps('schoolInfo')?.school_id,
          });
          if (res.success) {
            onOk(res.data);
            message.success('添加成功');
          } else {
            formPanelObject.reVaildation(res);
          }
          return false;
        }}
        onReset={(formPanelObject: FormPanelObject) => {
          formPanelObject.resetFields();
          onCancel();
        }}
        template={[
          {
            title: '',
            labelCol: { span: 6 },
            wrapperCol: { span: 18 },
            span: 24,
            inputs: [
              {
                label: '组织名称',
                name: 'org_name',
                key: 'org_name',
                required: true,
              },
            ],
          },
        ]}
      />
    </Modal>
  );
};
export default Groups;
